How to create a WordPress shortcode plugin?
It is pretty easy to roll out a WordPress plugin that adds a shortcode.
Creating a WordPress shortcode plugin
Here are the basic steps:
- Create a new file called MyPlugin.php
- Add this code:
0102030405060708091011121314151617
<?php
/*
Plugin Name: <Your Plugin Name>
Version: 1.0
Plugin URI: tba
Description:
Author: <your name>
Author URI: <your web site>
*/
function
handleShortcode(
$atts
,
$content
) {
return
"Hello, World!"
;
}
$test
= add_shortcode(
'my-shortcode'
,
'handleShortcode'
);
?>
- Upload (or copy) MyPlugin.php to the /wp-content/plugins/ directory in your WordPress install.
Using a WordPress shortcode plugin
- Start a new Post
- type in the following:
[my-shortcode]
- Click Preview.
Your post should have replaced your shortcode with “Hello, Word!”.
A better WordPress shortcode plugin template
While the above is all you need, a more scalable solution might involve using classes. Here is a template that uses classes.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php /* Plugin Name: <Your Plugin Name> Version: 1.0 Plugin URI: tba Description: Author: <your name> Author URI: <your web site> */ // A class to manage your plugin class MyPlugin { public function MyPlugin( $shortCodeHandler ) { $result = add_shortcode( 'my-shortcode' , array ( $shortCodeHandler , 'handleShortcode' ) ); } } // A class to handle your shortcode class ShortCodeHandler { public function handleShortcode( $atts , $content ) { return "Hello, World" ; } } $shortCodeHandler = new ShortCodeHandler(); $plugin = new MyPlugin( $shortCodeHandler ); ?> |
www.hermagazinehotsprings.com
How to create a WordPress shortcode plugin? | Rhyous